import requests
import uuid

# 高德地图 key
key = "19b9fc5d076c763a466702ffcef184aa"


def get_response(code, message, data):
    response = {
        'code': code,
        'message': message,
        'data': data
    }

    return response


def parameters_judge(require, request):
    if sorted(require) == sorted(request):
        return True
    else:
        return False


def parameters_missing():
    response = {
        'code': 1,
        'message': '缺少参数，或参数有误',
        'data': None
    }

    return response


def user_missing():
    response = {
        'code': 1,
        'message': '无此用户',
        'data': None
    }

    return response


def constellation_judge(month, day):
    constellation_map = {
        3: [(3, 21), (4, 20)],  # 白羊
        4: [(4, 21), (5, 20)],  # 金牛
        5: [(5, 21), (6, 20)],  # 双子
        6: [(6, 21), (7, 22)],  # 巨蟹
        7: [(7, 23), (8, 22)],  # 狮子
        8: [(8, 23), (9, 22)],  # 处女
        9: [(9, 23), (10, 22)],  # 天秤
        10: [(10, 23), (11, 22)],  # 天蝎
        11: [(11, 23), (12, 22)],  # 射手
        1: [(1, 22), (2, 19)],  # 水瓶
        2: [(2, 20), (3, 20)],  # 双鱼
    }

    # 摩羯单独判断 因为摩羯刚好跨年了
    if (month, day) >= (12, 22) or (month, day) <= (1, 19):
        return 12

    for key, value in constellation_map.items():
        if value[0] <= (month, day) <= value[1]:
            return key


def get_delay_time(origin, destination):
    origin_code, destination_code = get_code(origin, destination)
    time = get_time(origin_code, destination_code)
    return int(time/2)


def get_code(origin, destination):
    # 将参数放到字典里，之后可以直接在request.get中添加
    parameter = {
        'address': origin + '|' + destination,
        'key': key,
        'batch': True
    }
    geo_url = "https://restapi.amap.com/v3/geocode/geo?"
    response = requests.get(geo_url, parameter)
    # response 返回的是 'requests.models.response' 类型，要转为dic格式
    response = response.json()
    # geocodes 存放了两点的信息，为list类型，而list里的元素为dic类型
    # location 里面就是城市对应的 经纬度
    geocodes = response.get('geocodes')
    origin_code = geocodes[0].get('location')
    destination_code = geocodes[1].get('location')
    return origin_code, destination_code


def get_time(origin, destination):
    parameter = {
        'origins': origin,
        'destination': destination,
        'key': key,
    }
    dis_url = 'https://restapi.amap.com/v3/distance?'
    response = requests.get(dis_url, parameter)
    response = response.json()
    result = response.get('results')
    time = result[0].get('duration')
    # time 单位是秒，str类型 要转成小时
    time = float(time) / 3600
    return time


def user_avatar_path(instance, filename):
    """自定义用户头像保存路径和文件名"""
    # 获取源文件名的后缀
    ext = filename.split('.')[-1]
    # 通过当前时间字符串作为文件名
    file_name = str(uuid.uuid1())
    # 拼接文件名和后缀
    file = file_name + '.' + ext
    # 使用当前用户id为路径
    return file


def getAge(now, birth):
    if now.month > birth.month:
        return now.year - birth.year
    elif now.month == birth.month:
        if now.day >= birth.day:
            return now.year - birth.year
        else:
            return now.year - birth.year + 1
    else:
        return now.year - birth.year + 1
